/*
 * @Author: lwHao
 * @Date: 2020-10-23 16:13:27
 * @LastEditors: lwHao
 * @LastEditTime: 2020-10-23 16:27:42
 */
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export const createStore = () => {
  return new Vuex.Store({
    state: () => {
      posts: []
    },
    mutations: {
      setPosts (state, data) {
        state.posts = data
      }
    },
    actions: {
      // 在服务端选人期间务必让 action 返回一个 Promise
      async getPosts ({ commit }) {
        const  { data } = await axios.get('https://cnodejs.org/api/v1/topics')
        commit('stePosts', data.data)
      }
    }
  })
}